package com.tim.app.ui.fragment.selceted;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.library.base.BaseFragment;
import com.application.library.net.StringResponseCallback;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.Friend;
import com.tim.app.server.result.InviteToAnswerResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.adapter.selceted.InviteToAnswerAdapter;
import com.tim.app.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 邀请列表页面
 */
public class InviteToAnswerFragment extends BaseFragment implements View.OnClickListener, LoadMoreHandler {

    public static final String TAG = "InviteToAnswerFragment";
    private View rootView;

    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvFriend;
    private EmptyLayout emptyLayout;
    private InviteToAnswerAdapter adapter;
    private List<Friend> dataList;

    private String problemId;
    private int type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_invite_to_answer, null);

            load_more = (LoadMoreRecycleViewContainer) rootView.findViewById(R.id.load_more);
            wrvFriend = (WrapRecyclerView) rootView.findViewById(R.id.wrvFriend);

            wrvFriend.setOverScrollMode(View.OVER_SCROLL_NEVER);
            load_more.useDefaultFooter(View.GONE);
            load_more.setAutoLoadMore(true);
            load_more.setLoadMoreHandler(this);

            emptyLayout = new EmptyLayout(getActivity(), load_more);
            emptyLayout.showLoading();
            emptyLayout.setEmptyButtonShow(false);
            emptyLayout.setEmptyDrawable(R.drawable.ic_empty_friend);
            emptyLayout.setEmptyText("你还没有关注其它用户", "");

            if (dataList == null) {
                dataList = new ArrayList<>();
            }
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            wrvFriend.setLayoutManager(layoutManager);


            Bundle bundle = getArguments();
            problemId = bundle.getString("problemId");
            type = bundle.getInt("type");

            adapter = new InviteToAnswerAdapter(getActivity(), dataList, problemId, type);
            wrvFriend.setAdapter(adapter);
            initData(problemId, type);
        }
        return rootView;
    }

    private int pageNumber = 1;

    private void initData(String problemId, int type) {
        if (type == AppKey.INVITE_TYPE_FRIEND) {
            API_User.ins().getFriendList(TAG, problemId, pageNumber, AppKey.PAGE_SIZE, refreshCallback);
        } else if (type == AppKey.INVITE_TYPE_RECOMMAND) {
            API_User.ins().getRecommandInvitedList(TAG, problemId, refreshCallback);
        }
    }

    private void loadMoreData(String problemId, int type) {
        if (type == AppKey.INVITE_TYPE_FRIEND) {
            API_User.ins().getFriendList(TAG, problemId, pageNumber + 1, AppKey.PAGE_SIZE, refreshCallback);
        }
    }

    StringResponseCallback refreshCallback = new StringResponseCallback() {
        @Override
        public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
            if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                if (dataList != null) {
                    dataList.clear();
                }
                InviteToAnswerResult result = new Gson().fromJson(resultValue, InviteToAnswerResult.class);
                if (null != result && null != result.getRes() && null != result.getRes().getArraylist()) {
                    if (null != result.getRes().getArraylist().getResult()) {
                        dataList.addAll(result.getRes().getArraylist().getResult());
                    }
                    adapter.notifyDataSetChanged();
                    if (type == AppKey.INVITE_TYPE_RECOMMAND) {
                        load_more.loadMoreFinish(false, false);
                    } else {
                        pageNumber = result.getRes().getArraylist().getCurrentPageNo();
                        if (pageNumber != result.getRes().getArraylist().getTotalPageCount()) {
                            load_more.loadMoreFinish(false, true);
                        } else {
                            load_more.loadMoreFinish(false, false);
                        }
                    }
                }

                if (dataList == null || dataList.size() == 0) {
                    emptyLayout.showEmpty();
                } else {
                    emptyLayout.showContent();
                }
            } else {
                emptyLayout.showEmpty();
                ToastUtil.showToast(errMsg);
            }
            return false;
        }
    };

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView&& null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        loadMoreData(problemId, type);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


}
